<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="tool.getDevice() === 'mobile' ? '100%' : '600px'"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      
      <a-form-item label="等级名称" field="name">
        <a-input v-model="formData.name" placeholder="请输入等级名称" />
      </a-form-item>
      <a-form-item label="等级Logo" field="logo">
        <sa-upload-image v-model="formData.logo" :limit="1" :multiple="false" />
      </a-form-item>
      <a-form-item label="升级金额" field="upgrade_amount">
        <a-input v-model="formData.upgrade_amount" placeholder="请输入升级金额" />
      </a-form-item>
      <a-form-item label="等级金额" field="grading_amount">
        <a-input v-model="formData.grading_amount" placeholder="请输入等级金额" />
      </a-form-item>
      <a-form-item label="等级有效期" field="days">
        <a-input v-model="formData.days" placeholder="请输入等级有效期" />
      </a-form-item>
      
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import api from '@/api/user'

const emit = defineEmits(['success'])

// 引用定义
const formRef = ref()
const mode = ref('')
const visible = ref(false)
const loading = ref(false)

let title = computed(() => {
  return '级别管理' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单初始值
const initialFormData = {
  name: '',
  logo: '',
  upgrade_amount: '',
  grading_amount: '',
  days: '',
  id:''
}

// 验证规则
const rules = {
  name: [{ required: true, message: '等级名称不能为空' }],
  upgrade_amount: [{ required: true, message: '升级金额不能为空' }],
  grading_amount: [{ required: true, message: '等级金额不能为空' }],
  days: [{ required: true, message: '等级有效期不能为空' }],
}


// 表单信息
const formData = reactive({ ...initialFormData })



// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  // 重置表单数据
  Object.assign(formData, initialFormData)
  formRef.value.clearValidate()
  visible.value = true
}


// 设置数据
const setFormData = async (data) => {
  console.log(data)
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.addLevel(data)
    } else {
      // 修改数据
      result = await api.editLevel(data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
